package com.yanhui.utils.http;

import com.yanhui.utils.string._stringUtils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class _cmsUtils {
	public static void main(String[] args) {
		String a = "dsfdsf.js?v80";
		a = a.substring(0,a.lastIndexOf(".js"));
		System.out.println(a);
		
	}

	public static String webStatic(String urlStr, String filePath){
		String result = "";
		try {
            URL url = new URL(urlStr);
            URLConnection connection = url.openConnection();
            InputStream in = connection.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in,"utf-8"));
            StringBuffer sBuffer = new StringBuffer();
            while(reader.ready()){
                sBuffer.append(reader.readLine()+"\n");
            }
            File file = new File(filePath);
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),"utf-8"));
            result = sBuffer.toString();
            writer.write(result);
            writer.close();
            reader.close();
            in.close();
        } catch (Exception e) {
        	e.printStackTrace();
        }
		return result;
	}
	

	public static String getHtmlResourceByUrl(String url){
		return getHtmlResourceByUrl(url, "UTF-8");
	}
	public static String getHtmlResourceByUrl2(String url){
		return getHtmlResourceByUrl2(url, "UTF-8");
	}

	/**
	 * 获取网页源码
	 * @param url
	 * @param encoding
	 * @return
	 */
	public static String getHtmlResourceByUrl(String url,String encoding){
		StringBuffer buffer = new StringBuffer();
		URL urlObj = null;
		URLConnection uc = null;
		InputStreamReader in = null;
		BufferedReader reader = null;
		try {
			//建立网络连接
			urlObj = new URL(url);
			//打开网络连接
			uc = urlObj.openConnection();
			//创建输入流
			in = new InputStreamReader(uc.getInputStream(),encoding);
			//创建缓冲写入文件流
			reader = new BufferedReader(in);
			String line = null;
			while((line = reader.readLine())!=null){
				//一行一行的追加
				buffer.append(line+"\r\n");
			}
		} catch (Exception e) { 
				e.printStackTrace();
		}finally{
			try {
				if(in != null){
					in.close();
					}
				} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
		return buffer.toString();
	}
	public static String getHtmlResourceByUrl2(String url,String encoding){
		StringBuffer buffer = new StringBuffer();
		URL urlObj = null;
		URLConnection uc = null;
		InputStreamReader in = null;
		BufferedReader reader = null;
		try {
			//建立网络连接
			urlObj = new URL(url);
			//打开网络连接
			uc = urlObj.openConnection();
			//创建输入流
			in = new InputStreamReader(uc.getInputStream(),encoding);
			//创建缓冲写入文件流
			reader = new BufferedReader(in);
			String line = null;
			while((line = reader.readLine())!=null){
				//一行一行的追加
				buffer.append(line+"<br>");
			}
		} catch (Exception e) { 
			e.printStackTrace();
		}finally{
			try {
				if(in != null){
					in.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
		return buffer.toString();
	}


	public static List<String> getCode(String htmlSource, String title, String pat){
		String[] titles = _stringUtils.strToArray(title, ",");
		List<String> list = new ArrayList<String>();
		htmlSource = htmlSource.replaceAll("\\s", "");
		pat = pat.replaceAll("\\s", "");
		Pattern pattern1 = Pattern.compile(pat);
		Matcher matcher1 = pattern1.matcher(htmlSource);
		while(matcher1.find()){
			String mGroupStr = "";
			for(int i = 0 ,len = titles.length; i<len; i++){
				mGroupStr += matcher1.group(i+1) + ",";
			}
			mGroupStr = mGroupStr.substring(0,mGroupStr.length()-1);
			list.add(mGroupStr);
		}
		return list;
	}

	public static void downLoadUrl(String filePath, String downUrl) {
		String fileName = downUrl.substring(downUrl.lastIndexOf("/"));
		File files = new File(filePath);
		if(!files.exists()){
			files.mkdirs();
		}
		try {
			URL url = new URL(downUrl);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setConnectTimeout(30000);
			connection.setReadTimeout(30000);
			InputStream is = connection.getInputStream();
			File file = new File(filePath+"/"+fileName);
			FileOutputStream out = new FileOutputStream(file);
			int i = 0;
			while((i=is.read())!= -1){
				out.write(i);
			}
			is.close();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static String doHttpGet(String url, String encoding) throws Exception{
		String result = "";
		URL urlGet = new URL(url);
		HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
		http.setRequestMethod("GET"); // 必须是get方式请求
		http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
		http.setDoOutput(true);
		http.setDoInput(true);
		http.connect();
		InputStream is = http.getInputStream();
		int size = is.available();
		byte[] jsonBytes = new byte[size];
		is.read(jsonBytes);
		String message = new String(jsonBytes, encoding);
		result = message;
		is.close();
		return result;
	}
	


	
}
